﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ETFK
{
    public class DataToString
    {
        private delegate string DelegateToString(object obj);
        private static Dictionary<Type, DataProcess> Items = new Dictionary<Type, DataProcess>();

        public static void Init()
        {
            DataProcess dpBase = new DataProcess();
            dpBase.HandleToString = new DelegateToString(BaseTypeToString);

            Items.Add(typeof(int), dpBase);
            Items.Add(typeof(Int16), dpBase);
            Items.Add(typeof(Int64), dpBase);
            Items.Add(typeof(IntPtr), dpBase);

            Items.Add(typeof(Single), dpBase);

            Items.Add(typeof(double), dpBase);
        }

        private static string BaseTypeToString(object obj)
        {
            return obj.ToString();
        }

        public static string GetString(object obj)
        {
            if(Items.Count == 0)
            {
                Init();
            }

            Type typ = obj.GetType();
            if(Items.ContainsKey(typ)== true)
            {
                return Items[typ].GetString(obj);
            }

            return "没有针对数据类型" + typ.FullName + "定义相应的字串转换代码，请联系系统管理员！";
        }

        private class DataProcess
        {
            public DelegateToString HandleToString = null;

            public string GetString(object obj)
            {
                if(HandleToString !=null)
                {
                    return HandleToString(obj);
                }else
                {
                    return "数据" + obj.GetType().FullName + "未能转换为字符串形式！";
                }
            }
        }
    }
}
